package com.example.itcompanyinterview;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import com.example.itcompanyinterview.object.SubCategory;
import com.example.itcompanyinterview.tools.CommonJsonParser;
import com.example.itcompanyinterview.tools.CommonUrlRequest;
import com.example.itcompanyinterview.tools.CustomTitleSetter;
import com.example.itcompanyinterview.tools.LoadingDialogHelper;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class SubCategoryContentActivity extends Activity {

	private SubCategory _subCategory;
	private Handler _messageHandler;
	private LoadingDialogHelper _loadingHelper;
	private CustomTitleSetter _titleSetter;

	private static String CONTENT_CONTROLER = "SubCategory";
	private static String GET_SUBCATEGORY_CONTENT_ACTION = "GetSubCategoryContent";
	private static int GET_SUBCATEGORY_CONTENT = 0x03;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_sub_category_content);
		this.Initialize();
		this.getContentFromUrl();
	}
	
	private void Initialize() {
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		this._subCategory = (SubCategory)bundle.getSerializable("subcategory");
		
		this._titleSetter = new CustomTitleSetter(this);
		this._titleSetter.setTitle(this._subCategory.getName(), View.GONE, View.VISIBLE);
		
		this._loadingHelper = new LoadingDialogHelper(this);
		this.initMessageHandler();
	}
	
	private void initMessageHandler() {
		this._messageHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == GET_SUBCATEGORY_CONTENT) {
					_loadingHelper.closeLoadingDialog();
					String contentJsonString = (String) msg.obj;
					if (contentJsonString == null) {
						Toast.makeText(SubCategoryContentActivity.this,
								"获取内容失败，请检测网络", Toast.LENGTH_SHORT).show();
						return;
					}
					initContentDetail(contentJsonString);
				}
				super.handleMessage(msg);
			}
		};
	}
	
	private void initContentDetail(String contentJsonString) {
		try {
			TextView SubCategoryContentView = (TextView) this
					.findViewById(R.id.category_content);

			JSONObject jsonContentObj = CommonJsonParser
					.parseJsonObject(contentJsonString);
			if (jsonContentObj.getInt("err") != 0) {
				Toast.makeText(SubCategoryContentActivity.this,
						jsonContentObj.getString("msg"),
						Toast.LENGTH_LONG).show();
				return;
			}

			JSONObject jsonContent = CommonJsonParser
					.parseJsonObject(jsonContentObj.getString("value"));
			String subCategoryContent = jsonContent.getString("content");
			SubCategoryContentView.setText(subCategoryContent);
			this._subCategory.setContent(subCategoryContent);
		} catch (JSONException e) {
			Toast.makeText(SubCategoryContentActivity.this, "获取题目异常",
					Toast.LENGTH_LONG).show();
			Log.e("InitQuestionContent", e.getMessage());
		}
	}

	private void getContentFromUrl() {
		new Thread(new Runnable() {
			public void run() {
				List<Pair<String, String>> paramList = new ArrayList<Pair<String, String>>();
				CommonUrlRequest.setParam(paramList, "sub_cid", 
						String.valueOf(_subCategory.getId()));
				String problemJsonString = CommonUrlRequest
						.getControllerAndAction(
								SubCategoryContentActivity.CONTENT_CONTROLER,
								SubCategoryContentActivity.GET_SUBCATEGORY_CONTENT_ACTION,
								paramList);
				Message m = _messageHandler.obtainMessage();
				m.obj = problemJsonString;
				m.what = SubCategoryContentActivity.GET_SUBCATEGORY_CONTENT;
				_messageHandler.sendMessage(m);
			}
		}).start();
		this._loadingHelper.showLoadingDialog();
	}
}
